package com.leapp.sk.viewdelegate.task

import android.app.Activity
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
import android.widget.*
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.ContainsEmojiEditText
import com.example.mvp.view.NoScrollGridView
import com.example.mvp.view.SKTopBar
import com.example.util.ISTECHER
import com.leapp.sk.R
import com.leapp.sk.bean.CompleteTaskEntity
import com.leapp.sk.ui.activity.task.CompleteTaskActivity
import com.leapp.sk.ui.adapter.CompleteTaskAdapter
import com.leapp.sk.util.im.listener.LKVoiceRecorderListener
import com.leapp.sk.view.TaskRecordSoundView

/**
 * 完成作业
 * @author  chaohaipeng  on 2018/8/3.
 */
class CompleteTaskDelegate: BaseAppDelegate(){
    /**
     * 语音列表
     */
    private var lv_act_list:ListView?=null
    /**
     * 语音适配器
     */
    private var voiceAdapter:CompleteTaskAdapter?=null
    /**
     * 语音录制
     */
    private var tsv_act: TaskRecordSoundView?=null
    /**
     * 图片视频列表
     */
    private var gv_act_list:NoScrollGridView?=null
    /**
     * 图片视频适配器
     */
    private var ivAdapter:CompleteTaskAdapter?=null
    /**
     * 作业要求父布局
     */
    private var ll_ltr_rootView:LinearLayout?=null
    /**
     * 作业要求拓展按钮
     */
    private var iv_act_expand:ImageView?=null

    /**
     * 滑动布局
     */
    private var sv_act_list:ScrollView?=null

    /**
     * 文本内容
     */
    private var et_ltch_task:ContainsEmojiEditText?=null

    /**
     * 文本标题
     */
    private var et_ltch_title:ContainsEmojiEditText?=null
    /**
     * 文本标题
     */
    private var et_act_title:ContainsEmojiEditText?=null

    /**
     * 文本内容
     */
    private var et_act_task:ContainsEmojiEditText?=null

    override fun initWidget() {
    }
    override fun initLayoutRes(): Int {
        return R.layout.activity_complete_task
    }
    /**
     * 初始化语音作业适配器
     */
    fun initVoiceAdapter(mlist: MutableList<CompleteTaskEntity>){
        lv_act_list=getView<ListView>(R.id.lv_act_list)
         voiceAdapter = CompleteTaskAdapter(getActivity<AppCompatActivity>(), mlist,false)
        lv_act_list?.adapter=voiceAdapter
        lv_act_list?.addHeaderView(getHeadView())
    }
    /**
     * 初始化图片视频作业适配器
     */
    fun initIVAdapter(mlist: MutableList<CompleteTaskEntity>){
      val  ll_act_title=  getView<LinearLayout>(R.id.ll_act_title)
        sv_act_list=  getView(R.id.sv_act_list)
        et_act_title=  getView(R.id.et_act_title)
        et_act_task=  getView(R.id.et_act_task)
        gv_act_list=  getView(R.id.gv_act_list)
        val  tv_asn_check_request=  getView<TextView>(R.id.tv_asn_check_request)
        if (ISTECHER){
            ll_act_title.visibility=View.VISIBLE
            tv_asn_check_request.visibility=View.GONE
            et_act_task?.hint=getActivity<AppCompatActivity>().resources.getString(R.string.string_task_complete_hint_request_content)
        }else{
            ll_act_title.visibility=View.GONE
            tv_asn_check_request.visibility=View.VISIBLE
            et_act_task?.hint=getActivity<AppCompatActivity>().resources.getString(R.string.string_task_complete_hint_request)
        }

         ivAdapter = CompleteTaskAdapter(getActivity(), mlist,false)
        gv_act_list?.adapter=ivAdapter
        et_act_task?.setOnClickListener(View.OnClickListener {
            setBottomINVisible()
        })
    }


    /**
     * 设置班级
     */
    fun setClass(classS:String?){
        if (!TextUtils.isEmpty(classS)){
            val rl_act_class_data = getView<RelativeLayout>(R.id.rl_act_class_data)
            rl_act_class_data.visibility= View.VISIBLE
            val tv_act_class_data = getView<TextView>(R.id.tv_act_class_data)
            tv_act_class_data.text=classS
        }

    }

    /**
     * 刷新适配器
     */
    fun notifyVoiceAdapter(){
        lv_act_list?.visibility=View.VISIBLE
        sv_act_list?.visibility=View.GONE
        voiceAdapter?.notifyDataSetChanged()
        ivAdapter?.notifyDataSetChanged()
    }
    /**
     * 刷新适配器
     */
    fun notifyIvAdapter(){
        lv_act_list?.visibility=View.GONE
        sv_act_list?.visibility=View.VISIBLE
        ivAdapter?.notifyDataSetChanged()
        voiceAdapter?.notifyDataSetChanged()
    }

    /**
     * 初始化语音
     */
    fun initVoiceView(activity: Activity, mLKVoiceRecorderListener: LKVoiceRecorderListener){
        tsv_act=  getView(R.id.tsv_act)
        ll_ltr_rootView= getView(R.id.ll_ltr_rootView)
        iv_act_expand= getView(R.id.iv_act_expand)
        tsv_act?.initEvent(activity,mLKVoiceRecorderListener)
    }

    /**
     * 语音按钮显示
     */
    fun setVoiceVisible(){
        if (tsv_act?.visibility== View.VISIBLE){
            tsv_act?.visibility=View.GONE
        }else{
            tsv_act?.visibility=View.VISIBLE
            ll_ltr_rootView?.visibility=View.GONE
            iv_act_expand?.isSelected=false
        }
    }
    /**
     * 隐藏所有底部框
     */
    fun setBottomINVisible(){
        tsv_act?.visibility=View.GONE
        ll_ltr_rootView?.visibility=View.GONE
        iv_act_expand?.isSelected=false
    }

    /**
     * 设置语音时间
     */
    fun setDuration(mDuration:Int){
        tsv_act?.initDuration(mDuration)
    }



    /**
     * 作业要求设置
     */
    fun setTaskRequest(){
        if (ll_ltr_rootView?.visibility== View.VISIBLE){
            ll_ltr_rootView?.visibility=View.GONE
            iv_act_expand?.isSelected=false
        }else{
            ll_ltr_rootView?.visibility=View.VISIBLE
            iv_act_expand?.isSelected=true
            tsv_act?.visibility=View.GONE
        }
    }
    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        val jtb_act= getView<SKTopBar>(R.id.jtb_act_task)
        if (!TextUtils.isEmpty(title)){
            jtb_act?.setTitle(title)
        }
        setEvent(jtb_act)
    }
    /**
     * 设置设置事件
     */
   private fun setEvent(topbar:SKTopBar?){
        topbar?.setRightTVContent(getActivity<CompleteTaskActivity>().resources.getString(R.string.string_class_submit))
        topbar?.setRightTVColor(ContextCompat.getColor(getActivity<CompleteTaskActivity>(),R.color.color_19AD19))
        topbar?.getTv_ljtb_right_back()?.visibility=View.VISIBLE
        topbar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<CompleteTaskActivity>().finish()
            }
            override fun rightOnClick() {
                if (ivAdapter?.count!! >0||voiceAdapter?.count!!>0||!TextUtils.isEmpty(et_ltch_task?.text.toString())||!TextUtils.isEmpty(et_act_task?.text.toString())){
                    LKToastUtil.showToastShort("提交成功")
                }else{
                    LKToastUtil.showToastShort("请完成作业")
                }
            }
        })
    }
    /**
     * 获取头布局
     */
    private fun getHeadView():View {
        val tabView = View.inflate(getActivity<CompleteTaskActivity>(), R.layout.layout_task_complete_head, null)
        et_ltch_task= tabView.findViewById<ContainsEmojiEditText>(R.id.et_ltch_task)
        et_ltch_title= tabView.findViewById<ContainsEmojiEditText>(R.id.et_ltch_title)
       val ll_ltch_title= tabView.findViewById<LinearLayout>(R.id.ll_ltch_title)
        if (ISTECHER){
            ll_ltch_title.visibility=View.VISIBLE
            et_ltch_task?.hint=getActivity<AppCompatActivity>().resources.getString(R.string.string_task_complete_hint_request_content)
        }else{
            ll_ltch_title.visibility=View.GONE
            et_ltch_task?.hint=getActivity<AppCompatActivity>().resources.getString(R.string.string_task_complete_hint_request)
        }
        et_ltch_task?.setOnClickListener(View.OnClickListener {
            setBottomINVisible()
        })

        return tabView
    }


}